﻿$(function () {
    var pageSize = 15;
    var flot1;
    var flot2;
    var flot3;
    var flot4;
    
    $('#find').click(function () {
    	selectPage(1);
    	//$('#list').show();
    });
    
    
    function selectPage(pageCode) {
    	
    	var startDate = $('#startDate').val();
    	var endDate = $('#endDate').val();
/*    	var carno = $('#carno').val();
    	var pname = $('#pname').val();
    	var options = $("#area option:selected");
    	var area = options.val();*/
    	//var area = $('#area').val();

    	if(startDate==""||startDate==null){
    		
//    		$('body').alert({
//    		type: 'warning',
//    		content:'开始日期不能为空!'
//    		});
//    		return;
			swal("开始时间不能为空!");
			return;

    	}
    	if(endDate==""||endDate==null){
    		
//    		$('body').alert({
//    		type: 'warning',
//    		content:'结束日期不能为空!'
//    		});
//    		return;
			swal("结束时间不能为空!");
			return;
    	}
    	

        //分页查询 pageCode：页数
        $.ajax({
			type : "post",
			url : "/ProOperate/dailyReportByDate",
			data:{"pageIndex":pageCode,"pageSize":pageSize,"startDate":startDate,"endDate":endDate},
			dataType : "json",
			success : function(data) {
//				var bill_objRecords = eval("(" + redata.result + ")");
//				maxItems = eval("(" + redata.count + ")");
				$("#detail").html("");
				$("#detail #data").html("");
                //总页数
                $("#countPage").text(data.totalPage);
                $("#totalCount").text(data.totalCount);
//				for (var i = 0; i < data.result.length; i++) {
//
//                    $("#detail").append(
//                        "<tr>\n" +
//                        "<td>"+data.result[i].ticketno+"</td>\n" +
//                        "<td>"+data.result[i].carno+"</td>\n" +
//                        //"<td>"+dateFormat(data.result[i].grosstime,"")+"</td>\n" +
//                        "<td>"+data.result[i].grosstime+"</td>\n" +
//                        "<td>"+data.result[i].netweight+"</td>\n" +
//                        "<td>"+data.result[i].productname+"</td>\n" +
//                        "<td>"+data.result[i].username+"</td>\n" +
//                        "<td>"+data.result[i].iccode+"</td>\n" +
//                        "</tr>"
//                    );
//				}
                // //初始化进料数据分析
                var databuf1 = [];
                var databuf2 = [];
                var databuf3 = [];
                var databuf4 = [];
                var data1 = [];
                var data2 = [];
                var data3 = [];
                var data4 = [];
                var data5 = [];
                var data6 = [];
                var data7 = [];
                var data8 = [];
                var data9 = [];
                var data10 = [];
                var data11 = [];
                for (var i = 0; i < data.result.length; i++) {
                    data1.push([data.result[i].tdate, data.result[i].jl_cc]);
                    data2.push([data.result[i].tdate, data.result[i].jl_dgy]);
                    data3.push([data.result[i].tdate, data.result[i].jl_hgy]);
                    data4.push([data.result[i].tdate, data.result[i].jl_wn_jk]);
                    data5.push([data.result[i].tdate, data.result[i].jl_wn_qt]);
                    data6.push([data.result[i].tdate, data.result[i].nh_e]);
                    data7.push([data.result[i].tdate, data.result[i].nh_zls_cq]);
                    data8.push([data.result[i].tdate, data.result[i].nh_zls_zb]);
                    data9.push([data.result[i].tdate, data.result[i].cp_zq_cs]);
                    data10.push([data.result[i].tdate, data.result[i].cp_zq_wg]);
                    data11.push([data.result[i].tdate, data.result[i].cp_zq_zy]);
                }
                databuf1.push(data1);
                databuf1.push(data2);
                databuf1.push(data3);
                databuf1.push(data4);
                databuf1.push(data5);
                databuf4.push(data6);
                databuf2.push(data7);
                databuf2.push(data8);
                databuf3.push(data9);
                databuf3.push(data10);
                databuf3.push(data11);
                var labels1 = ["餐厨", "地沟油","火锅油","京口污泥","其他污泥"];
                var colors1 = ['#87cefa', '#ff001e','#ff8c00','#d9ff00','#59ff00'];
                var labels2 = ["自来水厂区","自来水总表"];
                var colors2 = ['#87cefa', '#0073ff'];
                var labels3 = ["沼气产生", "沼气外供","沼气自用"];
                var labels4 = ["电量"];
                var colors3 = ['#87cefa', '#ff001e','#d9ff00'];
                var colors4 = ['#87cefa'];
                var borderColor = '#efefef';
                var bgColor = '#fff';
                analysisdb_jll("#website-stats1", databuf1, labels1, colors1, borderColor, bgColor);
                analysisdb_nh_w("#website-stats2", databuf2, labels2, colors2, borderColor, bgColor);
                analysisdb_nh_e("#website-stats4", databuf4, labels4, colors4, borderColor, bgColor);
                analysisdb_cpcl("#website-stats3", databuf3, labels3, colors3, borderColor, bgColor);

			}
		});
    }

    function analysisdb_jll(selector, data, labels, colors, borderColor, bgColor) {

        flot1 = $.plot($(selector),
            [   { data: data[0],
                    label: labels[0],
                    color: colors[0],
                },
                { data: data[1],
                    label: labels[1],
                    color: colors[1],
                },
                { data: data[2],
                    label: labels[2],
                    color: colors[2],
                },
                { data: data[3],
                    label: labels[3],
                    color: colors[3],
                },
                { data: data[4],
                    label: labels[4],
                    color: colors[4],
                }
            ],
            {
                series: {
                    lines: {
                        show: true,
                        fill: true,
                        lineWidth: 1,
                        fillColor: {
                            colors: [ { opacity: 0.1 },
                                { opacity: 0.1 }
                            ]
                        }
                    },
                    points: {
                        show: true
                    },
                    shadowSize: 0
                },
                legend: {
                    position: 'nw'
                },
                grid: {
                    hoverable: true,
                    clickable: true,
                    borderColor: borderColor,
                    borderWidth: 1,
                    labelMargin: 10,
                    backgroundColor: bgColor
                },
                yaxis: {
                    min: 0,
                    max: 160,
                    color: 'rgba(0,0,0,0.1)',
                    tickDecimals:2
                },
                xaxis: {
                    color: 'rgba(0,0,0,0.1)',
                    mode: "time",
                    timeformat: "%m-%d",
                    minTickSize: [1, "day"],
                    timezone: "browser",
                },

                tooltip: true,
                tooltipOpts: {
                    content: '%x%s数值： %y',
                    shifts: {
                        x: -60,
                        y: 25
                    },
                    defaultTheme: false
                }
            });
    }

    function analysisdb_nh_w(selector, data, labels, colors, borderColor, bgColor) {

        flot2 = $.plot($(selector),
            [   { data: data[0],
                label: labels[0],
                color: colors[0],
            },
                { data: data[1],
                    label: labels[1],
                    color: colors[1],
                }
            ],
            {
                series: {
                    lines: {
                        show: true,
                        fill: true,
                        lineWidth: 1,
                        fillColor: {
                            colors: [ { opacity: 0.1 },
                                { opacity: 0.1 }
                            ]
                        }
                    },
                    points: {
                        show: true
                    },
                    shadowSize: 0
                },
                legend: {
                    position: 'nw'
                },
                grid: {
                    hoverable: true,
                    clickable: true,
                    borderColor: borderColor,
                    borderWidth: 1,
                    labelMargin: 10,
                    backgroundColor: bgColor
                },
                yaxis: {
                    min: 0,
                    max: 200,
                    color: 'rgba(0,0,0,0.1)',
                    tickDecimals:2
                },
                xaxis: {
                    color: 'rgba(0,0,0,0.1)',
                    mode: "time",
                    timeformat: "%m-%d",
                    minTickSize: [1, "day"],
                    timezone: "browser",

                },

                tooltip: true,
                tooltipOpts: {
                    content: '%x%s数值： %y',
                    shifts: {
                        x: -60,
                        y: 25
                    },
                    defaultTheme: false
                }
            });
    }

    function analysisdb_nh_e(selector, data, labels, colors, borderColor, bgColor) {

        flot4 = $.plot($(selector),
            [   { data: data[0],
                label: labels[0],
                color: colors[0],
            }
            ],
            {
                series: {
                    lines: {
                        show: true,
                        fill: true,
                        lineWidth: 1,
                        fillColor: {
                            colors: [ { opacity: 0.2 },
                                { opacity: 0.2 }
                            ]
                        }
                    },
                    points: {
                        show: true
                    },
                    shadowSize: 0
                },
                legend: {
                    position: 'nw'
                },
                grid: {
                    hoverable: true,
                    clickable: true,
                    borderColor: borderColor,
                    borderWidth: 1,
                    labelMargin: 10,
                    backgroundColor: bgColor
                },
                yaxis: {
                    min: 0,
                    max: 10000,
                    color: 'rgba(0,0,0,0.1)',
                    tickDecimals:2
                },
                xaxis: {
                    color: 'rgba(0,0,0,0.1)',
                    mode: "time",
                    timeformat: "%m-%d",
                    minTickSize: [1, "day"],
                    timezone: "browser",

                },

                tooltip: true,
                tooltipOpts: {
                    content: '%x%s数值： %y',
                    shifts: {
                        x: -60,
                        y: 25
                    },
                    defaultTheme: false
                }
            });
    }

    function analysisdb_cpcl(selector, data, labels, colors, borderColor, bgColor) {

        flot3 = $.plot($(selector),
            [   { data: data[0],
                label: labels[0],
                color: colors[0],
            },
                { data: data[1],
                    label: labels[1],
                    color: colors[1],
                },
                { data: data[2],
                    label: labels[2],
                    color: colors[2],
                }
            ],
            {
                series: {
                    lines: {
                        show: true,
                        fill: true,
                        lineWidth: 1,
                        fillColor: {
                            colors: [ { opacity: 0.1 },
                                { opacity: 0.1 }
                            ]
                        }
                    },
                    points: {
                        show: true
                    },
                    shadowSize: 0
                },
                legend: {
                    position: 'nw'
                },
                grid: {
                    hoverable: true,
                    clickable: true,
                    borderColor: borderColor,
                    borderWidth: 1,
                    labelMargin: 10,
                    backgroundColor: bgColor
                },
                yaxis: {
                    min: 0,
                    max: 10000,
                    color: 'rgba(0,0,0,0.1)',
                    tickDecimals:2
                },
                xaxis: {
                    color: 'rgba(0,0,0,0.1)',
                    mode: "time",
                    timeformat: "%m-%d",
                    minTickSize: [1, "day"],
                    timezone: "browser",

                },

                tooltip: true,
                tooltipOpts: {
                    content: '%x%s数值： %y',
                    shifts: {
                        x: -60,
                        y: 25
                    },
                    defaultTheme: false
                }
            });
    }

    //数据点点击事件，点击后隐藏/显示
    $("#website-stats1").bind("plotclick", function (event, pos, item) {
        if (item) {
            var seriesIndex = item.seriesIndex;
            var series = flot1.getData()[seriesIndex];

            // 切换显示/隐藏状态
            series.lines.show = !series.lines.show;

            // 重新绘制图表
            flot1.setData(flot1.getData());
            flot1.draw();
        }
    });
    $("#website-stats2").bind("plotclick", function (event, pos, item) {
        if (item) {
            var seriesIndex = item.seriesIndex;
            var series = flot2.getData()[seriesIndex];

            // 切换显示/隐藏状态
            series.lines.show = !series.lines.show;

            // 重新绘制图表
            flot2.setData(flot2.getData());
            flot2.draw();
        }
    });
    $("#website-stats3").bind("plotclick", function (event, pos, item) {
        if (item) {
            var seriesIndex = item.seriesIndex;
            var series = flot3.getData()[seriesIndex];

            // 切换显示/隐藏状态
            series.lines.show = !series.lines.show;

            // 重新绘制图表
            flot3.setData(flot3.getData());
            flot3.draw();
        }
    });
    $("#website-stats4").bind("plotclick", function (event, pos, item) {
        if (item) {
            var seriesIndex = item.seriesIndex;
            var series = flot4.getData()[seriesIndex];

            // 切换显示/隐藏状态
            series.lines.show = !series.lines.show;

            // 重新绘制图表
            flot4.setData(flot4.getData());
            flot4.draw();
        }
    });

   
   
    /**
     * 日期格式化
     * @param Number time 时间戳
     * @param String format 格式
     */
    function dateFormat (time, format) {
      const t = new Date(time)
      // 日期格式
      format = format || 'Y-m-d h:i:s'
      let year = t.getFullYear()
      // 由于 getMonth 返回值会比正常月份小 1
      let month = t.getMonth() + 1
      let day = t.getDate()
      let hours = t.getHours()
      let minutes = t.getMinutes()
      let seconds = t.getSeconds()

      const hash = {
        'y': year,
        'm': month,
        'd': day,
        'h': hours,
        'i': minutes,
        's': seconds
      }
      // 是否补 0
      const isAddZero = (o) => {
        return /M|D|H|I|S/.test(o)
      }
      return format.replace(/\w/g, o => {
        let rt = hash[o.toLocaleLowerCase()]
        return rt >= 10 || isAddZero(o) ? rt : `0${rt}`
      })
    }

    //日报导出到本地
    $('#dailyreportlocal').click(function () {
        $.ajax({
            type : "post",
            url : "/ProOperate/dailyExcelOutFromDbLocal",
            dataType : "json",
            success : function(data) {
                if(data.result===1){

                    swal("导出成功！", "日报已保存至桌面！");
                }
                else if(data.result===0){
                    swal("导出失败！", "当前文件正在被打开，请先关闭！");
                }
                else if(data.result===-1){
                    swal("导出失败！", "当日数据未填写！");
                }
                else{
                    swal("导出失败！", "未知异常，请重新尝试！");
                }


            }
        });
    });

    //日报导出到浏览器
    $('#dailyreportexplorer').click(function () {
        window.location.href = "/ProOperate/dailyExcelOutFromDbExplorer";
    });

    //月报导出到本地
    $('#weeklyreport').click(function () {
        $.ajax({
            type : "post",
            url : "/ProOperate/weeklyExcelOutLocal",
            dataType : "json",
            success : function(data) {
                if(data.result===1){

                    swal("导出成功！", "周报已保存至桌面！");
                }
                else if(data.result===0){
                    swal("导出失败！", "当前文件正在被打开，请先关闭！");
                }
                else{
                    swal("导出失败！", "请重新尝试！");
                }

            }
        });
    });

    //周报导出到浏览器
    $('#weeklyreportexplorer').click(function () {
        window.location.href = "/ProOperate/weeklyExcelOutExplorer";
    });

    //导出日报
    function daily() {
        $.ajax({
            type : "post",
            // url : "/WeightBridge/dailyExcelOut",
            url : "/ProOperate/dailyExcelOutFromDb",
            dataType : "json",
            success : function(data) {
                if(data.result==1){

                    swal("导出成功！", "日报已保存至桌面！");
                }
                else if(data.result==0){
                    swal("导出失败！", "当前文件正在被打开，请先关闭！");
                }
                else{
                    swal("导出失败！", "请重新尝试！");
                }


            }
        });
    }
//导出周报
    function weekly() {
        $.ajax({
            type : "post",
            url : "/ProOperate/weeklyExcelOut",
            dataType : "json",
            success : function(data) {
                if(data.result==1){

                    swal("导出成功！", "周报已保存至桌面！");
                }
                else if(data.result==0){
                    swal("导出失败！", "当前文件正在被打开，请先关闭！");
                }
                else{
                    swal("导出失败！", "请重新尝试！");
                }


            }
        });
    }

})